﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MarginTrading.Entity;
using MarginTrading.Services;
using Microsoft.Reporting.WinForms;
using MarginTrading.Common;

namespace MarginTrading.Forms
{
    public partial class TrialBalance : Form
    {
        private class Item
        {
            public string Name;
            public int Value;
            public Item(string name, int value)
            {
                Name = name; Value = value;
            }
            public override string ToString()
            {
                // Generates the text shown in the combo box
                return Name;
            }
        }

        public TrialBalance()
        {
            InitializeComponent();
        }

        private void TrialBalance_Load(object sender, EventArgs e)
        {

        }

        private void LoadReport(DateTime date)
        {
            try
            {

                ReportDataSource ds = new ReportDataSource();
                ds.Name = "master";

                ds.Value = new DataTable();


                Config AgentCodeConfig = ConfigServices.GetConfigByName("Agent Code");
                string AgentCode = "cts";
                if (AgentCodeConfig != null)
                {
                    AgentCode = AgentCodeConfig.Value;
                }


                double bankAccBalance = LoanAccountServices.GetBankAccBalance(AgentCode, date);
                double loanAccBalance = LoanAccountServices.GetLoanAccBalance(AgentCode, date);
                double accruedInterest = DailyInterestServices.GetAccruedInterest(date, AgentCode);
                double interestIncome = DailyInterestServices.GetInterestIncome(date, AgentCode);
                double creditTotal = interestIncome + (bankAccBalance < 0 ? bankAccBalance * -1 : 0) + (loanAccBalance < 0 ? loanAccBalance * -1 : 0);
                double debitTotal = accruedInterest + (bankAccBalance > 0 ? bankAccBalance : 0) + (loanAccBalance > 0 ? loanAccBalance : 0);

                
                AgentCode = Reports.AGENT_CODE;

                this.reportViewer1.LocalReport.ReportPath = "TrialBalance.rdlc";
                this.reportViewer1.LocalReport.DataSources.Clear();
                this.reportViewer1.LocalReport.DataSources.Add(ds);

                this.reportViewer1.LocalReport.SetParameters(new ReportParameter[] {
                        new ReportParameter("ImageLink",AgentCode.ToLower()),
                        new ReportParameter("Date1",date == DateTime.MinValue ? "" : date.ToString(DateFormat.DATE_FORMAT)),
                        new ReportParameter("CompanyName",Reports.COMPANY_NAME),
                        new ReportParameter("ReportTitle",string.Format(Reports.TRIAL_BALANCE,date.ToString(DateFormat.DATE_FORMAT))),
                        new ReportParameter("BankAccBalanceCredit",bankAccBalance < 0 ? (bankAccBalance*-1).ToString("0.00") : "0.00"),
                        new ReportParameter("BankAccBalanceDebit",bankAccBalance > 0 ? bankAccBalance.ToString("0.00") : "0.00"),
                        new ReportParameter("LoanAccBalanceCredit",loanAccBalance < 0 ? (loanAccBalance*-1).ToString("0.00") : "0.00"),
                        new ReportParameter("LoanAccBalanceDebit",loanAccBalance > 0 ? loanAccBalance.ToString("0.00") : "0.00"),
                        new ReportParameter("AccruedInterest",accruedInterest == 0 ? "0.00" : accruedInterest.ToString("0.00")),
                        new ReportParameter("InterestIncome",interestIncome ==0 ? "0.00": interestIncome.ToString("0.00")),
                        new ReportParameter("CreditTotal",creditTotal == 0 ? "0.00" : creditTotal.ToString("0.00")),
                        new ReportParameter("DebitTotal",debitTotal == 0 ? "0.00" : debitTotal.ToString("0.00"))});

                this.reportViewer1.RefreshReport();
                reportViewer1.SetDisplayMode(DisplayMode.PrintLayout);
                reportViewer1.ZoomMode = ZoomMode.Percent;
                reportViewer1.ZoomPercent = 100;

            }
            catch (System.Exception e)
            {
                ErrorLog.WriteToEventLog(e.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            LoadReport(dateTimePicker1.Value);
            button1.Enabled = true;
        }
    }
}
